//
//  Mp4SingleViewController.m
//  FitTime
//
//  Created by dllo on 16/4/5.
//  Copyright © 2016年 赵建平. All rights reserved.
//

#import "Mp4SingleViewController.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#import "ZHX_AVPlayer.h"
#import <MediaPlayer/MediaPlayer.h>

@interface Mp4SingleViewController ()

@property (nonatomic, strong) ZHX_AVPlayer *player;
@property (nonatomic, assign) BOOL isPlaying;
@property (nonatomic, strong) UISlider *slider;
@property (nonatomic, strong) UISlider *brightnessSlider;//亮度
@property (nonatomic, strong) UISlider *soundSlider;//音量
@property (nonatomic, strong) NSTimer *avTimer;
@property (nonatomic, strong) UIButton *playButton;

@property (nonatomic, strong) UIView *transparentView;
@property (nonatomic, strong) UIView *videoView;
@property (nonatomic, strong) UILabel *leftTimeLable;
@property (nonatomic, strong) UILabel *rightTimeLable;
@property (nonatomic, strong) UIButton *boundsButton;
@property (nonatomic, strong) UIButton *returnButton;
@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@property (nonatomic, strong) UIView *upView;
@property (nonatomic, strong) UIView *downView;
@property (nonatomic, assign) BOOL isAlpha;
@property (nonatomic, strong) UIButton *totalButton;//全部按钮
@property (nonatomic, strong) UIView *rightView;

@property(nonatomic) CGFloat brightness NS_AVAILABLE_IOS(5_0);//屏幕亮度


@end

@implementation Mp4SingleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor blackColor];
//    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    
    
    [self createTransparentUIView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.navigationController.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBar.hidden = NO;
}


#pragma mark - 创建透明UIView
- (void)createTransparentUIView {
    
    self.transparentView = [[UIView alloc] init];
    self.transparentView.frame = CGRectMake((SCREENWIDTH - SCREENHEIGHT) / 2, (SCREENHEIGHT - SCREENWIDTH) / 2, SCREENHEIGHT, SCREENWIDTH);
    self.transparentView.transform = CGAffineTransformMakeRotation(M_PI_2);
    [self.view addSubview:self.transparentView];
    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]
                                  init];
    [tap addTarget:self action:@selector(tapAction:)];
    [self.transparentView addGestureRecognizer:tap];

    [self createPlayer];
}

#pragma mark - 创建播放页面
- (void)createPlayer {
        
        //    设置layer
        self.player = [ZHX_AVPlayer shareAVPlayer] ;
        [_player playWithUrl:self.urlString];
        self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
        _playerLayer.frame = CGRectMake(0, 0, SCREENHEIGHT , SCREENWIDTH);
        
        [self.transparentView.layer addSublayer:_playerLayer];
        
        //    设置显示图层视频的宽高比自动适应播放窗口的大小
        _playerLayer.videoGravity = AVLayerVideoGravityResizeAspect;
   
    [self createUpView];
    [self createReturnButton];
    [self createTotalButton];
    [self createDownView];
    [self createPlayButton];
    [self createSlider];
    [self createTimeLable];
    
}
#pragma mark - 创建顶部View
- (void)createUpView {
    self.upView = [UIView new];
    self.upView.backgroundColor = [UIColor blackColor];
    self.upView.alpha = 0.5;
    
    self.upView.frame = CGRectMake(0, 0, SCREENHEIGHT, 50);
    [self.transparentView addSubview:self.upView];
}

#pragma mark - 创建返回按钮
- (void)createReturnButton {
    
    self.returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.upView addSubview:_returnButton];
    
    self.returnButton.frame = CGRectMake(10, 10, 30, 30);
    [_returnButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:UIControlStateNormal];

    
    [self.view bringSubviewToFront:_returnButton];
    [_returnButton addTarget:self action:@selector(returnButtonAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)returnButtonAction:(UIButton *)button {
    self.isPlaying = NO;
    [self.player pause];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - 创建全部按钮
- (void)createTotalButton {
    
    self.totalButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.totalButton.frame = CGRectMake(self.upView.frame.size.width - 50, 10, 30, 30);
    
    [self.totalButton setBackgroundImage:[UIImage imageNamed:@"pages"] forState:UIControlStateNormal];
    [self.totalButton addTarget:self action:@selector(totalButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.upView addSubview:self.totalButton];
}

- (void)totalButtonAction:(UIButton *)totalButton {
    
    self.rightView = [[UIView alloc] init];
    self.rightView.frame = CGRectMake(self.transparentView.frame.size.width, 0, self.transparentView.frame.size.height- self.transparentView.frame.size.width, self.transparentView.frame.size.height);
    self.rightView.backgroundColor = [UIColor blackColor];
    self.rightView.alpha = 0.5;
    [self.transparentView addSubview:self.rightView];

    self.upView.hidden = YES;
    self.downView.hidden = YES;
    
    [self createBrightnessSlider];
    [self createSuondSlider];
}

#pragma mark - 调节屏幕亮度的slider
- (void)createBrightnessSlider {
    
    self.brightnessSlider = [[UISlider alloc] init];
    self.brightnessSlider.frame = CGRectMake(10,self.rightView.frame.size.height /10 , self.rightView.frame.size.width - 20, 10);
    
    self.brightnessSlider.minimumValueImage = [UIImage imageNamed:@"small"];
    self.brightnessSlider.maximumValueImage = [UIImage imageNamed:@"big"];
    
    
    [self.brightnessSlider setThumbImage:[UIImage imageNamed:@"page"] forState:UIControlStateNormal];
    
    [self.rightView addSubview:self.brightnessSlider];
    
    //    设置最大值最小值
    self.brightnessSlider.minimumValue = 0.0f;
    self.brightnessSlider.maximumValue = 1.0f;

    self.brightnessSlider.value = 1;
    
    [self.brightnessSlider addTarget:self action:@selector(brightnessSliderAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)brightnessSliderAction:(UISlider *)brightnessSlider {
    
    //屏幕亮度
    [[UIScreen mainScreen] setBrightness: self.brightnessSlider.value];

}

- (void)createSuondSlider {
    self.soundSlider = [[UISlider alloc] init];
    self.soundSlider.frame = CGRectMake(10,self.rightView.frame.size.height /5 , self.rightView.frame.size.width - 20, 10);
    
    [self.soundSlider setThumbImage:[UIImage imageNamed:@"page"] forState:UIControlStateNormal];
    self.soundSlider.minimumValueImage = [UIImage imageNamed:@"smallsound"];
    self.soundSlider.maximumValueImage = [UIImage imageNamed:@"bigsound"];
    
    [self.rightView addSubview:self.soundSlider];
    
    //    设置最大值最小值
    self.soundSlider.minimumValue = 0.0f;
    self.soundSlider.maximumValue = 1.0f;
    
    self.soundSlider.value = 0.2f;
    
    [self.soundSlider addTarget:self action:@selector(soundSliderAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)soundSliderAction:(UISlider *)soundSlider {
    //    设置音量
    self.player.volume = soundSlider.value;
}

#pragma mark - 创建底部View
- (void)createDownView {
    self.downView = [UIView new];
    self.downView.backgroundColor = [UIColor blackColor];
    self.downView.alpha = 0.5;
    self.downView.frame = CGRectMake(0, SCREENWIDTH - 50, SCREENHEIGHT, 50);
    [self.transparentView addSubview:self.downView];

}

#pragma mark - 创建播放按钮
- (void)createPlayButton {
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.playButton.frame = CGRectMake(15, 10, 30, 30);
    [self.downView addSubview:self.playButton];
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
    
    [self.playButton addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 开始按钮点击事件
- (void)playButton:(UIButton *)button {
    if (self.isPlaying) {
        //        暂停
        [self.player pause];
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
        
        self.isPlaying = NO;
    } else {
        [self.player play];
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"pausea"] forState:UIControlStateNormal];
        self.avTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(avTimerAction) userInfo:nil repeats:YES];
        self.isPlaying = YES;
    }
}

- (void)avTimerAction{
    
    self.slider.value = CMTimeGetSeconds(self.player.currentItem.currentTime) / CMTimeGetSeconds(self.player.currentItem.duration);
    NSInteger totalTime = CMTimeGetSeconds(self.player.currentItem.currentTime);
    NSInteger minTime = totalTime / 60;
    NSInteger secTime = totalTime % 60;
    NSString *timeString = [NSString stringWithFormat:@"%02ld:%02ld", (long)minTime, (long)secTime];
    
    self.leftTimeLable.text = timeString;
    self.leftTimeLable.textAlignment = NSTextAlignmentCenter;
    
}

#pragma mark - 创建进度条
- (void)createSlider {

    self.slider = [[UISlider alloc] init];
    self.slider.frame = CGRectMake(self.returnButton.frame.size.width + 20, self.returnButton.frame.size.height / 2 + 5, self.downView.frame.size.width - self.returnButton.frame.size.width - 30, 10);

    [self.slider setThumbImage:[UIImage imageNamed:@"page"] forState:UIControlStateNormal];
    [self.downView addSubview:self.slider];

    //    设置最大值最小值
    self.slider.minimumValue = 0.0f;
    self.slider.maximumValue = 1.0f;
  
    [self.slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - slider事件
- (void)sliderAction:(UISlider *)slider {
    //    参数1：跳转到的事件
    //    参数2：每秒多少针
    //    总帧数 / 每秒帧数 * slider的百分比
    if (self.player.currentItem.duration.value) {
        [self.player seekToTime:CMTimeMakeWithSeconds(self.player.currentItem.duration.value / self.player.currentItem.duration.timescale * self.slider.value, self.player.currentTime.timescale) completionHandler:^(BOOL finished) {
            [self.player play];
        }];
    }
    
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"pausea"] forState:UIControlStateNormal];
    self.isPlaying = YES;
}


#pragma mark - 创建左右侧时间
- (void)createTimeLable {
    
    self.leftTimeLable = [UILabel new];

    [self.downView addSubview:self.leftTimeLable];
    
    [self.leftTimeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.slider.mas_top).offset(-5);
        make.left.equalTo(self.slider.mas_left);
        make.height.mas_equalTo(@20);
    }];
    
    NSString *timeString = [NSString stringWithFormat:@"00:00"];
    
    self.leftTimeLable.textColor = [UIColor whiteColor];
    self.leftTimeLable.text = timeString;
    self.leftTimeLable.textAlignment = NSTextAlignmentCenter;
    
    NSString *string = [NSString stringWithFormat:@"%.2ld:%.2ld", (long)self.time / 60, (long)self.time % 60];
    self.rightTimeLable = [UILabel new];
    [self.downView addSubview:self.rightTimeLable];
    [self.rightTimeLable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.slider.mas_right);
        make.bottom.equalTo(self.slider.mas_top).offset(-5);
        make.height.mas_equalTo(@20);
    }];
    
    self.rightTimeLable.textColor = [UIColor whiteColor];
    self.rightTimeLable.text = string;
    self.rightTimeLable.textAlignment = NSTextAlignmentCenter;

}

#pragma mark - 轻拍
- (void)tapAction:(UITapGestureRecognizer *)tap {
    
    if (self.isAlpha) {
        self.upView.hidden = YES;
        self.downView.hidden = YES;
        self.isAlpha = NO;
    } else {
        self.upView.hidden = NO;
        self.downView.hidden = NO;
        self.isAlpha = YES;
    }

    [UIView transitionWithView:self.rightView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
        
       self.rightView.frame = CGRectMake(self.transparentView.frame.size.height, 0, self.transparentView.frame.size.height- self.transparentView.frame.size.width, self.transparentView.frame.size.height);
    } completion:^(BOOL finished) {
    }];
    
}








/*
#pragma mark - 创建全屏按钮
- (void)createBounds {
    self.boundsButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.transparentView addSubview:self.boundsButton];

    [self.boundsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.transparentView.mas_right).offset(-10);
        make.bottom.equalTo(self.transparentView.mas_bottom).offset(-20);
        make.width.mas_equalTo(@30);
        make.height.mas_equalTo(@30);
    }];
    
    [self.boundsButton setBackgroundImage:[UIImage imageNamed:@"bounds"] forState:UIControlStateNormal];
    [self.boundsButton addTarget:self action:@selector(boundsButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}
 
#pragma mark - 全屏按钮点击事件
- (void)boundsButtonAction:(UIButton *)button {
    
    self.transparentView.transform = CGAffineTransformMakeRotation(M_PI_2);
    
    [self.transparentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_top);
        make.right.equalTo(self.view.mas_bottom);
        make.top.equalTo(self.view.mas_right);
        make.bottom.equalTo(self.view.mas_left);
    }];

    self.transparentView.backgroundColor = [UIColor cyanColor];
    _playerLayer.frame = CGRectMake((SCREENWIDTH - SCREENHEIGHT) / 2, (SCREENHEIGHT - SCREENWIDTH) / 2 - 150, SCREENHEIGHT, SCREENWIDTH);
    [self createUpView];
    [self createDownView];
}
 
*/


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
